package com.start.admin.mappers;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.start.admin.entity.StartSysMenu;
import com.start.admin.vo.StartSysUserMenuVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface SysMenuMapper extends BaseMapper<StartSysMenu> {
    String USER_MENU_SQL="select a.*  " +
            "from (  " +
            " SELECT sm.*  " +
            " FROM start_sys_menu sm  " +
            "  INNER JOIN start_sys_role_menu srm ON sm.id = srm.menu_id  " +
            "  INNER JOIN start_sys_user_role sur ON sur.role_id = srm.role_id  " +
            "     AND sur.user_id = #{userId}  " +
            " UNION  " +
            " select ssm.*  " +
            " from start_sys_menu ssm  " +
            " where ssm.dynamic_form_id is not null  " +
            "   and create_by = #{userId}) a  " +
            "ORDER BY a.sort, a.parent_id";
    @Select(USER_MENU_SQL)
    List<StartSysUserMenuVo> listByUserId(@Param("userId") String userId);

    /**
     * @param userId
     * @param wrapper
     * @return
     */
    @Select("select count(a.id) from ( " +USER_MENU_SQL+") a  ${ew.customSqlSegment}")
    Long userPermissionCount(@Param("userId") String userId, @Param("ew") Wrapper<StartSysUserMenuVo> wrapper);
}
